package cn.com.dhc.danlu.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.renderscript.Int2;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.gson.Gson;

import cn.com.dhc.danlu.dlware.global.DanluApplication;
import cn.com.hd.mall.web.webservices.entity.response.login.LoginResponse;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-15
 * Time: 09:56
 * Description: .....
 */
public class SerializeCache {
    private static final String SP_CACHE_NAME = "dl_cache";
    private static final String KEY_RESPONSE = "response";
    private static final String KEY_ROLECODE = "rolecode";
    private static final String KEY_ROLEID = "roleid";
    private static final String KEY_TOKEN = "token";
    private static final String KEY_ISTERMINAL = "isterminalrole";
    private static final String KEY_CATEGORY = "category";
    private static final String KEY_SESSION = "session";
    private static final String KEY_CATEGORY_ITEM = "category_item";

    private static SerializeCache sInstance;

    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    @SuppressLint ("CommitPrefEdits")
    private SerializeCache () {
        mSharedPreferences = DanluApplication.get ().getSharedPreferences (SP_CACHE_NAME, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit ();
    }

    public static SerializeCache getInstance () {
        if (null == sInstance) {
            synchronized (Object.class) {
                if (null == sInstance) {
                    sInstance = new SerializeCache ();
                }
            }
        }
        return sInstance;
    }

    public void saveSessionId(String sessionId){
        mEditor.putString (KEY_SESSION,sessionId);
        mEditor.commit ();
    }

    public String getSessionId() {
        return mSharedPreferences.getString (KEY_SESSION,"");
    }

    public void saveRoleCode(int roleCode) {
        mEditor.putInt (KEY_ROLECODE, roleCode);
        mEditor.commit ();
    }

    int getRoleCode () {
        return mSharedPreferences.getInt (KEY_ROLECODE, 0);
    }

    public void saveToken (String token) {
        mEditor.putString (KEY_TOKEN, token);
        mEditor.commit ();
    }

    public String getToken () {
        return mSharedPreferences.getString (KEY_TOKEN, "");
    }

    public void saveRoleId(String roleId) {
        mEditor.putString (KEY_ROLEID, roleId);
        mEditor.commit ();
    }

    public String getRoleId() {
        return mSharedPreferences.getString (KEY_ROLEID, "");
    }

    public void saveIsTerminal(boolean isTerminal) {
        mEditor.putBoolean (KEY_ISTERMINAL, isTerminal);
        mEditor.commit ();
    }

    public boolean isTerminalRole() {
        return mSharedPreferences.getBoolean (KEY_ISTERMINAL, false);
    }

    public void saveResponse (@NonNull LoginResponse response) {
        Gson gson = new Gson ();
        String s = gson.toJson (response);
        mEditor.putString (KEY_RESPONSE, s);
        mEditor.commit ();
    }

    public LoginResponse getResponse () {
        String jsonStr = mSharedPreferences.getString (KEY_RESPONSE, "");
        if (!TextUtils.isEmpty (jsonStr)) {
            Gson gson = new Gson ();
            return gson.fromJson (jsonStr, LoginResponse.class);
        }
        return null;
    }

    public void saveCategoryIndex (int categorySelectedIndex) {
        mEditor.putInt (KEY_CATEGORY, categorySelectedIndex);
        mEditor.commit ();
    }

    public void saveCategoryIndexs (int categorySelectedIndex, int contentSelectedIndex) {
        mEditor.putInt (KEY_CATEGORY, categorySelectedIndex);
        mEditor.putInt (KEY_CATEGORY_ITEM, contentSelectedIndex);
        mEditor.commit ();
    }
    public void saveCategorySecondIndexs ( int contentSelectedIndex) {
        mEditor.putInt (KEY_CATEGORY_ITEM, contentSelectedIndex);
        mEditor.commit ();
    }

    public int getCategoryIndex () {
        return mSharedPreferences.getInt (KEY_CATEGORY, -1);
    }

    //以Int2格式返回选择的位置，第一个为一级分类下标， 第二个为二级分类下标
    public Int2 getCategoryIndexs () {
        int categorySelectedIndex = mSharedPreferences.getInt (KEY_CATEGORY, -1);
        int contentSelectedIndex = mSharedPreferences.getInt (KEY_CATEGORY_ITEM, -1);
        Int2 indexs = new Int2(categorySelectedIndex, contentSelectedIndex);
        return indexs;
    }

    public void clear () {
        mEditor.clear ();
        mEditor.commit ();
    }
}